// -----------------------------------------------------
//  controls
// -----------------------------------------------------
// 0 - label
// 1 - textbox
// 2 - checkbox
// 3 - combo
// 4 - selector
// 5 - datepicker
// 6 - checklist


var EditableDetail_Instances = [];

function EditableDetail(id)
{
    this.superClase = EditableGroup;
    this.superClase(id);
    
    this.parentID;
    this.referenceID;
    this.items = [];
    this.links = [];

    this.trashIconPath = "";
    this.duplicateIconPath = "";

    this.clone = EditableDetail_Clone;
    this.copyValues = EditableDetail_CopyValues;
    this.show = EditableDetail_Show;
    this.dispose = EditableDetail_Dispose;
    this.addLink = EditableDetail_AddLink;
    
    this.addRowIDParameter = EntityDetail_AddRowIDParameter;
    
    EditableDetail_Instances[id] = this;    
} 

function EditableDetail_Clone(newid)
{
    var newDetail = new EditableDetail(newid);
    
    newDetail.referenceID = this.id;
    newDetail.items = this.items.slice();
    newDetail.links = this.links.slice();
    
    return newDetail;
}

function EditableDetail_CopyValues(detail)
{
    for(var i=0; i<this.items.length; i++) {
        this.setItemValue(i, detail.getItemValue(i));
        
        // Selectores
        if(this.items[i].type == 4)
            this.setItemDescription(i, detail.getItemDescription(i));
    }
}

function EditableDetail_Dispose()
{
    for(var i=0; i<this.items.length; i++) {
        var item = this.items[i];
        var controlId = this.id + "_" + item.id;
        
        ItemsCommon.disposeControl(controlId);
    }
}

// Agrega a la tabla las columnas que representaran el detalle
function EditableDetail_Show(tableContainer, removeButton, removeLabel, cloneButton, cloneLabel)
{
    var row = tableContainer.insertRow(tableContainer.rows.length);
    row.id = "row_" + this.id;
    
    for(var i=0; i<this.items.length; i++) {
        var cell = row.insertCell(row.cells.length);
        cell.className = "controlCell";
        
        var item = this.items[i];
        var controlId = this.referenceID + "_" + item.id;
        var newControlId = this.id + "_" + item.id;
        
        // Obtengo un control clonado del item de referencia
        // en el caso de los controles propios del Framework devuelve el html que representa el control 
        var newControl = ItemsCommon.getClonedControl(item.type, controlId, newControlId);
        cell.appendChild(newControl);
        
        // Textbox
        if (item.type == 1) {
            // Agrego a los parametros de la funcion OnChange el RowId        
            var textbox = ItemsCommon.getControl(item.type, controlId);
            var newTextbox = ItemsCommon.getControl(item.type, newControlId);
            $(newTextbox).attr("onchange", this.addRowIDParameter($(textbox).attr("onchange")));
        }
        
        // Combos y CheckList
        if( (item.type == 3) || (item.type == 6) ) {
            // Agrego a los parametros de la funcion OnChange del combo el RowId        
            var combo = ItemsCommon.getControl(item.type, controlId);
            var newCombo = ItemsCommon.getControl(item.type, newControlId);
            newCombo.onChange = this.addRowIDParameter(combo.onChange);
        }
            
        // Selectores       
        if(item.type == 4) {
            // Agrego a los parametros de las funciones OnSelect y OnUnselect del selector el RowId
            var selector = ItemsCommon.getControl(item.type, controlId);
            var newSelector = ItemsCommon.getControl(item.type, newControlId);
            newSelector.onSelect = this.addRowIDParameter(selector.onSelect);
            newSelector.onUnselect = this.addRowIDParameter(selector.onUnselect);
        }
                
        // Controles anidables        
        if( (item.type == 3) || (item.type == 4) || (item.type == 5) || (item.type == 6) ) {
            if(typeof(Entity_Relationships) != "undefined") {
                // Copio las relaciones
                var child = Entity_Relationships[controlId];
                if(child) {
                    var newChild = child.replace(this.referenceID + "_", this.id + "_");
                    Entity_Relationships[newControlId] = newChild;
                }    
            }
        }
    }
    
    if(removeButton || cloneButton || this.links.length > 0)
    {
        var innerHTML = "";
        var cellAction = row.insertCell(row.cells.length);
        cellAction.className = "controlCell";
        cellAction.style.textAlign = "center";
                
        // links
        for (var i = 0; i < this.links.length; i++) {
            var src = this.links[i][0];
            var text = this.links[i][1];
            var action = this.links[i][2];

            innerHTML += "<img title=\"" + text + "\" src=\"" + src + "\" style=\"cursor:pointer\" onclick=\"" + action + "('" + this.parentID + "', '" + this.id + "')\">";
        }
        
        // boton de eliminar
        if(removeButton)
            innerHTML += "<img title=\""+removeLabel+"\" src=\"" + this.trashIconPath + "\" style=\"cursor:pointer\" onclick=\"EditableDetails_RemoveItem('" + this.parentID + "', '" + this.id + "')\">";

        // boton duplicar
        if(cloneButton)
            innerHTML += "<img title=\""+cloneLabel+"\" src=\"" + this.duplicateIconPath + "\" style=\"cursor:pointer\" onclick=\"EditableDetails_DuplicateItem('" + this.parentID + "', '" + this.id + "')\">";
        
        cellAction.innerHTML = innerHTML;
    }
}

function EditableDetail_AddLink(iconPath, linkText, linkAction) {
    var newLink = [iconPath, linkText, linkAction];
    this.links.push(newLink);
}


// -----------------------------------------------------------------------------------------------------
//      Private Functions
// -----------------------------------------------------------------------------------------------------

function EntityDetail_AddRowIDParameter(functionCall)
{
    if( (!functionCall) || (functionCall=="") )
        return functionCall;
        
    var posQuote = functionCall.indexOf(",");
    var posOpen = functionCall.indexOf(")");
    var posClose = functionCall.indexOf(")");

    if (posQuote == -1 && posOpen + 1 < posClose)
        return functionCall.substring(0, posClose) + "'" + this.id + "')";    
    else
        return functionCall.substring(0, posClose) + ",'" + this.id + "')";      
}